import sys
import os
from PyQt4 import QtGui, QtCore
from config import config

class elogin(QtGui.QDialog):
    def __init__(self, info=None, data=None, parent=None, addinfo=None):
    #def __init__(self, parent=None):
        super(elogin, self).__init__(parent)
        self.info = info
        self.data = data
        #self.info = ''
        #self.data = ''
        data_add = config.read_config()
        if data_add.setting[0].show_error_log_detail == 1 :
            self.addinfo = "\n\n" + "-"*51 + """ Programming Development : \n
%s
        """ %(str(addinfo))
        elif data_add.setting[0].show_error_log_detail == None :
            self.addinfo = " "
        elif data_add.setting[0].show_error_log_detail == " " :
            self.addinfo = " "
        elif data_add.setting[0].show_error_log_detail == "" :
            self.addinfo = " "
        else:
            self.addinfo = " "

        if os.getcwd() != os.path.split(sys.argv[0])[0]:
            if "system" in os.path.split(sys.argv[0])[0]:
                mypath = os.path.split(sys.argv[0])[0] + "/error/images/"
            else:
                if "error" in os.path.split(sys.argv[0])[0]:
                    mypath = os.path.split(sys.argv[0])[0] + "/images/"
                else:
                    mypath = os.path.split(sys.argv[0])[0] + "/system/error/images/"
        else:
            if "system" in os.getcwd():
                if "error" in os.getcwd():
                    mypath = os.path.split(sys.argv[0])[0] + '/images/' 
                else:
                    mypath = os.path.split(sys.argv[0])[0] + '/error/images/' 
            else:
                mypath = os.path.split(sys.argv[0])[0] + "/system/error/images/"

        #print "mypath = ", mypath                #for test only
        #print "os.getcwd() = ", os.getcwd()      #for test only
        #print "sys.argv[0] = ", sys.argv[0]      #for test only

        self.setGeometry(300, 300, 550, 260)
        self.setFixedSize(500, 260)
        
        if self.info != None or self.info != '' or len(self.info) > 0:
            self.setWindowTitle(str(self.info).capitalize())
            if str(self.info).isalnum:
                self.setWindowIcon(QtGui.QIcon(mypath + 'info2.png'))
            else:
                self.setWindowIcon(QtGui.QIcon(mypath + self.info + '2.png'))
        else:
            self.setWindowTitle("Info")
            self.setWindowIcon(QtGui.QIcon(mypath + 'Info' + '2.png'))
        
        self.btClose = QtGui.QPushButton('Close', self)
        self.btClose.setGeometry(330,215,70, 30)
        self.connect(self.btClose, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))

        #self.btYes = QtGui.QPushButton('Yes', self)
        #self.btYes.setFocusPolicy(QtCore.Qt.NoFocus)

        #self.btNo = QtGui.QPushButton('No', self)
        #self.btNo.setFocusPolicy(QtCore.Qt.NoFocus)

        #self.btYes.move(280, 200)
        #self.btYes.connect(self.btYes, QtCore.SIGNAL('clicked()'), self.go_login)
        #self.setFocus()

        #self.btNo.move(400, 200)
        #self.connect(self.btNo, QtCore.SIGNAL('clicked()'), QtCore.SLOT('close()'))
        #self.setFocus()

        self.img_label = QtGui.QLabel(self)
        self.img_label.setMinimumSize(256, 256)
        self.img_label.setGeometry(0, 0, 256, 256)
        self.img_info = QtGui.QImage(mypath + str(self.info) + '.png')
        #self.img_info = QtGui.QImage(mypath + str(self.getInfo()) + '.png')
        self.img_label.setPixmap(QtGui.QPixmap.fromImage(self.img_info))

        self.label = QtGui.QTextEdit(self)
        self.label.setReadOnly(True)
        #self.label.setGeometry(258, 15, 230, 70)  #QLabel
        self.label.setGeometry(258, 7, 230, 205)
        #self.label.setText(str(self.data))  #QLabel
        self.label.setText(str(self.data) + self.addinfo)
        self.label.setFont(QtGui.QFont('Arial',8, 500))
        self.label.enabledChange(False)
        #self.label.setWordWrap(True) #QLabel

        self.center()
        self.setWindowModality(True)
        
    def setInfo(self, myinfo):
        self.info = myinfo
        
    def getInfo(self):
        return self.info
    
    def setData(self, mydata):
        self.data = mydata
        
    def getData(self):
        return self.data

    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size =  self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)

def main(info, data, addinfo): #test only !
    app = QtGui.QApplication(sys.argv)
    myapp = elogin(info,data, addinfo)
    #app.exec_()
    myapp.exec_()

#main('error','You has been logout !,\nDo you want to login\nagain ?', "kdfjgdkfgjdf") #test only

